#pragma once
#include  <iostream>
#include  <memory>
#include  <vector>
#include  <string>
#include  <algorithm>
#include "OptimizationStrategy.h"
#include "DoubleBufferingStrategy.h"
#include "BackupStrategy.h"
enum class OptimizationStrategyType {
    DoubleBufferingStrategy, // 双缓冲策略
    BackupBufferingStrategy,// 备份缓冲策略
};
inline std::string getStrategyType(OptimizationStrategyType type) {
   switch (type) {
   case OptimizationStrategyType::DoubleBufferingStrategy:
       return "DoubleBufferingStrategy";
   case OptimizationStrategyType::BackupBufferingStrategy:
       return "BackupBufferingStrategy";
   default:
       return "Unknown";
   }
}
// OptimizationManager类用于管理优化策略
class OptimizationManager {
public:
    // 获取单例实例的方法
    static OptimizationManager& getInstance() {
        static OptimizationManager instance;
        return instance;
    }
    void init();
    bool hasStrategy() const {
        return strategy != nullptr;
    }

    // 添加策略的方法
    void addStrategy(std::shared_ptr<OptimizationStrategy> strategy) {
        strategies.push_back(strategy);
    }

    // 删除策略的方法
    void removeStrategy(std::shared_ptr<OptimizationStrategy> strategy) {
        strategies.erase(std::remove(strategies.begin(), strategies.end(), strategy), strategies.end());
    }

    // 执行所有策略的方法
    void executeStrategies() {
//        for (const auto& strategy : strategies) {
//          //  strategy->execute();
//        }
    }
    void setStrategy(std::unique_ptr<OptimizationStrategy> newStrategy) {
        if (hasStrategy()) {
            // 提示用户已经设置了其他策略，或者不允许设置新的策略
        } else {
            strategy = std::move(newStrategy);
        }
    }
    void setStrategy(std::string strategyName) {
        if (hasStrategy()) {
            // 提示用户已经设置了其他策略，或者不允许设置新的策略
            if(strategy->getName() == strategyName){
                return;
            }
        }
        // 根据策略名称创建策略
        if(strategyName == "DoubleBufferingStrategy") {
            strategy = std::make_unique<DoubleBufferingStrategy>();
        } else if(strategyName == "BackupStrategy") {
            strategy = std::make_unique<BackupStrategy>();
        } else {
            // 提示用户策略名称不正确
            std::cout << "Unknown strategy name: " << strategyName << std::endl;
            return;
        }
        std :: cout << "Set strategy: " << strategyName << " - result : "<<strategy->getName()<< std::endl;
    }

private:
    // 私有构造函数和赋值运算符，防止被外部调用
    OptimizationManager() = default;
    OptimizationManager(const OptimizationManager&) = delete;
    OptimizationManager& operator=(const OptimizationManager&) = delete;

    std::vector<std::shared_ptr<OptimizationStrategy>> strategies;  // 存储策略的容器
    std::unique_ptr<OptimizationStrategy> strategy;  // 当前策略
};
